﻿//
// <author>David Nohejl</author>
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
using WPFTrees.Parser;

namespace WPFTrees.Layouts
{
    /// <summary>
    /// Nakresleni stromu ve kterem jsou hrany lomene cary.
    /// </summary>
    public class RectangularBranchDrawer:IBranchDrawer
    {
        /// <inheritdoc />       
        public System.Windows.FrameworkElement CreateBranch()
        {
            Polyline polyline = new Polyline();
            polyline.Points = new PointCollection(3);
            polyline.Points.Add(new Point());
            polyline.Points.Add(new Point());
            polyline.Points.Add(new Point());
            polyline.Stroke = Brushes.Black;
            polyline.StrokeThickness = 1.0;            
            return polyline;            
        }

        /// <inheritdoc />       
        public void PositionBranch(FrameworkElement branch, NewickNode root, 
            NewickNode node, Point rootPos, Point nodePos)
        {
            Polyline polyline = branch as Polyline;

            if (polyline == null)
            {
                throw new ArgumentException("Argument of type Polyline expected.",
                    "branch");
            }

            Point p1 = polyline.Points[0];
            Point p2 = polyline.Points[1];
            Point p3 = polyline.Points[2];

            node.VisualElement.UpdateLayout();
            p1.X = rootPos.X + (DefaultSettings.Default.DrawInnerNodes 
                                    ? DefaultSettings.Default.InnerNodeHeight / 2 
                                    : 0);
            p1.Y = rootPos.Y + node.VisualElement.ActualHeight / 2;
            p2.X = nodePos.X + (node.IsLeaf 
                                     ? DefaultSettings.Default.LeafNodeHeight / 2 
                                     : DefaultSettings.Default.DrawInnerNodes 
                                            ? DefaultSettings.Default.InnerNodeHeight / 2 
                                            : 0);
            p2.Y = rootPos.Y + node.VisualElement.ActualHeight / 2;
            p3.X = nodePos.X + (node.IsLeaf 
                                    ? DefaultSettings.Default.LeafNodeHeight / 2 
                                    : DefaultSettings.Default.DrawInnerNodes 
                                            ? DefaultSettings.Default.InnerNodeHeight / 2 
                                            : 0); ;
            p3.Y = nodePos.Y + node.VisualElement.ActualHeight / 2;

            polyline.Points = new PointCollection(new Point[] { p1, p2, p3 });
        }
    }
}
